<template>
  <div class="app-container">
    <!-- header -->
    <div class="filterContainer">
      <el-input v-model="keyword" placeholder="学院名称" style="width: 200px; margin-right: 10px" class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>
    <!-- content -->

    <div v-if="list.length == 0"
      style="display: flex;justify-content: center;align-items: center;color: #ccc;height: 500px;"
      v-loading="listLoading">
      No Data
    </div>
    <div class="cardContainer" v-else v-loading="listLoading">
      <el-card class="box-card" :data="list" element-loading-text="Loading" border fit highlight-current-row
        v-for="item in list" :key="item.institute_id" shadow="hover" :body-style="{ padding: '20px' }">
        <div slot="header" class="clearfix">
          <span>{{ item.institute_name }}</span>
          <el-button style="float: right; padding: 3px 0;color: brown;" type="text" @click="del(item)">删除</el-button>
          <el-button style="float: right; padding: 3px 0;margin-right: 20px;" type="text"
            @click="edit(item)">编辑</el-button>
        </div>
        <div v-if="item.grades.length == 0" style="color: #ccc;text-align: center;line-height: 120px;overflow: auto;">
          该学院暂时没有年级!
        </div>
        <div v-else style="height: 120px;overflow: auto;">
          <div v-for="g in item.grades" :key="g.grade_id" class="text item">
            {{ g.grade_name }}
          </div>
        </div>
      </el-card>
    </div>

    <!-- 编辑学院弹框 -->
    <el-dialog title="学院信息" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="dialogform">
        <el-form-item label="学院名称" prop="institute_name">
          <el-input v-model="form.institute_name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
          @click="dialogStatus === 'create' ? createData('dialogform') : confirmedit('dialogform')">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 分页 -->
    <div style="position: fixed;bottom: 0px;">
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
        layout="total, prev, pager, next, jumper" @pagination="fetchData" />
    </div>

  </div>
</template>

<script>
import {
  getInstitute,
  editInstitute,
  addInstitute,
  delInstitute,
  searchInstitute
} from "@/api/infotable";
import Pagination from "@/components/Pagination";
import { mapGetters } from "vuex";

export default {
  components: { Pagination },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      // 传递的页号
      listQuery: {
        pageNum: 1,
        pageSize: 4,
      },
      dialogFormVisible: false,
      form: {
        institute_id: '',
        institute_name: ''
      },
      // 中间量
      obj: null,
      rules: {
        institute_name: [
          { required: true, message: '学院不能为空', trigger: 'blur' }
        ],
      },
      dialogStatus: "",
      // 搜索
      keyword: ""
    };
  },
  created() {
    console.log(getInstitute);
    this.fetchData();
  },
  computed: {
    ...mapGetters(["sidebar", "avatar"]),
  },
  methods: {
    // 获取数组信息
    fetchData() {
      this.listLoading = true;
      getInstitute(this.listQuery).then((response) => {
        this.list = response.data.InList.map(item => {
          return { ...item, grades: response.data.GrList.filter(g => g.institute_id == item.institute_id) }
        })
        this.total = response.data.total;
        console.log(this.list);
        console.log(response.data);
        this.listLoading = false;
      });
    },
    // 编辑学院名称弹框
    edit(obj) {
      this.dialogStatus = 'edit'
      this.form.institute_id = obj.institute_id
      this.form.institute_name = obj.institute_name
      this.obj = obj
      this.dialogFormVisible = true
      console.log(obj);
    },
    // 确认编辑信息
    confirmedit() {
      if (this.obj.institute_name != this.form.institute_name) {
        editInstitute({ institute_id: this.form.institute_id }, { institute_name: this.form.institute_name })
          .then((response) => {
            console.log(response);
            if (response.code == 200) {
              this.fetchData()
              this.dialogFormVisible = false
            }
          })
      } else {
        this.fetchData()
        this.dialogFormVisible = false
      }
    },
    // 删除学院
    del(obj) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(async () => {
          if (obj.grades.length > 0) {
            this.$message.warning('该学院仍有年级,还不能删除哟!')
          } else {
            delInstitute({ institute_id: obj.institute_id })
              .then((response) => {
                console.log(response);
                if (response.code == 200) {
                  this.fetchData()
                }
              })
          }
        })
    },
    // 将内容进行重置
    reset() {
      this.form.institute_id = ''
      this.form.institute_name = ''
    },
    // 点击添加按钮触发--添加学院
    handleCreate() {
      this.reset()
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs['dialogform'].clearValidate();
      });
    },
    // 添加学院
    createData(form) {
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          addInstitute({ institute_name: this.form.institute_name })
            .then((response) => {
              console.log(response);
              if (response.code == 200) {
                this.fetchData()
                this.dialogFormVisible = false
              }
            })
        } else {
          this.$message.error('添加失败：存在未填项')
          return false;
        }
      });
    },
    // 搜索按钮
    handleFilter() {
      this.listLoading = true
      searchInstitute(this.listQuery, { keyword: this.keyword })
        .then((response) => {
          console.log(response);
          this.list = response.data.InList.map(item => {
            return { ...item, grades: response.data.GrList.filter(g => g.institute_id == item.institute_id) }
          })
          this.total = response.data.total;
          this.listLoading = false;
        })
    },
  },
  mounted() { },
};
</script>

<style scoped>
.filterContainer {
  margin-bottom: 10px;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.cardContainer {
  padding: 0 60px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  flex-wrap: wrap;
}

@media screen and (max-width: 800px) {
  .box-card {
    width: 500px;
    margin: 10px 0;
  }
}

@media screen and (min-width: 800px) {
  .box-card {
    width: 45%;
    margin: 10px 0;
  }
}
</style>